/****************************************************************************
 * Copyright (C) 2012
 * by Dj_Skual
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any
 * damages arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any
 * purpose, including commercial applications, and to alter it and
 * redistribute it freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you
 * must not claim that you wrote the original software. If you use
 * this software in a product, an acknowledgment in the product
 * documentation would be appreciated but is not required.
 *
 * 2. Altered source versions must be plainly marked as such, and
 * must not be misrepresented as being the original software.
 *
 * 3. This notice may not be removed or altered from any source
 * distribution.
 *
 * ManageWindow.cpp
 *
 * for SaveGame Manager GX 2012
 ***************************************************************************/
#include "ManageWindow.hpp"
#include "../Themes/CTheme.h"
#include "../Controls/Application.h"

#define SLIDE_SPEED 35

ManageWindow::ManageWindow()
	: GuiFrame(0, 0)
{
	selIndex = CurrentBrowser()->GetSelIndex();
	sliding = false;
	closing = false;
	
	DimBackground(true);
	
	trigA.SetSimpleTrigger(-1, WiiControls.ClickButton | ClassicControls.ClickButton << 16, GCControls.ClickButton);
	trigB.SetButtonOnlyTrigger(-1, WiiControls.BackButton | ClassicControls.BackButton << 16, GCControls.BackButton);
	trigL.SetButtonOnlyTrigger(-1, WiiControls.LeftButton | ClassicControls.LeftButton << 16, GCControls.LeftButton);
	trigR.SetButtonOnlyTrigger(-1, WiiControls.RightButton | ClassicControls.RightButton << 16, GCControls.RightButton);
	trigPlus.SetButtonOnlyTrigger(-1, WiiControls.NextButton | ClassicControls.NextButton << 16, 0);
	trigMinus.SetButtonOnlyTrigger(-1, WiiControls.PrevButton | ClassicControls.PrevButton << 16, 0);
	
	btnSoundClick = Resources::GetSound("button_click.wav");
	btnSoundOver = Resources::GetSound("button_over.wav");
	btnSoundClick->SetVolume(Settings.SFXVolume);
	btnSoundOver->SetVolume(Settings.SFXVolume);
	
	dialogBoxData = Resources::GetImageData("manage_save_bg.png");
	dialogBoxImg = new GuiImage(dialogBoxData);
	
	SetSize(dialogBoxImg->GetWidth(), dialogBoxImg->GetHeight());
	
	btnBoxData = Resources::GetImageData("manage_save_box_button.png");
	closeBoxImg = new GuiImage(btnBoxData);
	closeBoxImg->SetAlignment(ALIGN_RIGHT | ALIGN_TOP);
	closeBoxImg->SetPosition(-18, 6);
	
	btnClose = new PictureButton("button_close.png", "button_close_over.png", btnSoundClick, btnSoundOver);
	btnClose->SetAlignment(ALIGN_RIGHT | ALIGN_TOP);
	btnClose->SetPosition(-21, 8);
	btnClose->SetTrigger(&trigA);
	btnClose->SetTrigger(&trigB);
	btnClose->Clicked.connect(this, &ManageWindow::OnCloseButtonClick);
	
	btnLeft = new PictureButton("arrow_left.png", "arrow_left.png", btnSoundClick, btnSoundOver);
	btnLeft->SetAlignment(ALIGN_LEFT | ALIGN_MIDDLE);
	btnLeft->SetPosition(10, 0);
	btnLeft->SetTrigger(&trigA);
	btnLeft->SetTrigger(&trigL);
	btnLeft->SetTrigger(&trigMinus);
	btnLeft->Clicked.connect(this, &ManageWindow::OnArrowButtonClick);
	
	btnRight = new PictureButton("arrow_right.png", "arrow_right.png", btnSoundClick, btnSoundOver);
	btnRight->SetAlignment(ALIGN_RIGHT | ALIGN_MIDDLE);
	btnRight->SetPosition(-22, 0);
	btnRight->SetTrigger(&trigA);
	btnRight->SetTrigger(&trigR);
	btnRight->SetTrigger(&trigPlus);
	btnRight->Clicked.connect(this, &ManageWindow::OnArrowButtonClick);
	
	Append(dialogBoxImg);
	Append(closeBoxImg);
	Append(btnClose);
	Append(btnLeft);
	Append(btnRight);
	
	EffectFinished.connect(this, &ManageWindow::OnEffectFinish);
	
	SetAlignment(ALIGN_CENTER | ALIGN_MIDDLE);
	SetPosition(6, 0);
	SetEffect(EFFECT_SLIDE_TOP | EFFECT_SLIDE_IN, 50);
}

ManageWindow::~ManageWindow()
{
	RemoveAll();

	Resources::Remove(btnSoundClick);
	Resources::Remove(btnSoundOver);
	
	Resources::Remove(dialogBoxData);
	Resources::Remove(btnBoxData);
	
	delete dialogBoxImg;
	delete closeBoxImg;
	
	delete btnClose;
	delete btnLeft;
	delete btnRight;
}

void ManageWindow::OnCloseButtonClick(GuiButton *sender UNUSED, int pointer UNUSED, const POINT &p UNUSED)
{
	if(sliding)
		return;
	
	SetEffect(EFFECT_SLIDE_TOP | EFFECT_SLIDE_OUT, 50);
	Close();
}

void ManageWindow::OnArrowButtonClick(GuiButton *sender, int pointer UNUSED, const POINT &p UNUSED)
{
	if(sliding)
		return;
	sliding = true;
	
	int SlideEffectIN = 0;
	bool inc = false;
	
	if(sender == btnLeft)
	{
		SetEffect(EFFECT_SLIDE_RIGHT | EFFECT_SLIDE_OUT, SLIDE_SPEED);
		SlideEffectIN = EFFECT_SLIDE_LEFT | EFFECT_SLIDE_IN;
	}
	else
	{
		SetEffect(EFFECT_SLIDE_LEFT | EFFECT_SLIDE_OUT, SLIDE_SPEED);
		SlideEffectIN = EFFECT_SLIDE_RIGHT | EFFECT_SLIDE_IN;
		inc = true;
	}
	
	while(this->IsAnimated())
		Application::Instance()->updateEvents();
	
	Refresh(inc);
	
	if(!closing)
		SetEffect(SlideEffectIN, SLIDE_SPEED);
}

void ManageWindow::OnEffectFinish(GuiElement *e UNUSED)
{
	int effect = GetEffect();
	
	if ((effect == (EFFECT_SLIDE_LEFT | EFFECT_SLIDE_IN)) ||
		(effect == (EFFECT_SLIDE_RIGHT | EFFECT_SLIDE_IN)))
		sliding = false;
}

void ManageWindow::OnRefresh(bool inc)
{
	int entryCount = CurrentBrowser()->GetEntrieCount();
	if(!entryCount)
	{
		closing = true;
		Close();
		return;
	}
	
	if(inc)
	{
		selIndex++;
		
		if(selIndex >= entryCount)
			selIndex = 0;
	}
	else
	{
		selIndex--;
		
		if(selIndex < 0)
			selIndex = entryCount-1;
	}
	
	CurrentBrowser()->SetSelectedIndex(selIndex);
}

void ManageWindow::OnBlockButtons(bool block)
{
	int State = STATE_DEFAULT;
	if(block)
		State = STATE_DISABLED;
	
	btnClose->SetState(State);
	btnLeft->SetState(State);
	btnRight->SetState(State);
}
